<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib uri="/tld/dictionary" prefix="dictionary"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<title>可交易债权</title>
<link rel="stylesheet" href="${ctx}/static/css/style.css" type="text/css" /> 
<link rel="stylesheet" href="${ctx}/static/css/style1.css" type="text/css" /> 
<link rel="stylesheet" href="${ctx}/static/css/kongjian.css" type="text/css" />
<link rel="stylesheet" href="${ctx}/static/css/jquery.ui.all.css" />
<link rel="stylesheet" href="${ctx}/static/css/m.css" type="text/css" />
<script src="${ctx}/static/script/jquery-1.4.min.js" type="text/javascript"></script>
<script src="${ctx}/static/script/jquery.ui.core.js"></script>
<script src="${ctx}/static/script/jquery.ui.datepicker.js"></script>
<script src="${ctx}/static/script/jsCoreUtil.js"></script>
</head>
<body>
<form:form id="mainForm" action="${ctx}/backsection/backsection_tradable_query" modelAttribute="backSectionForm" method="post" onsubmit="doSubmit();return false;">
<c:set var="tradableMap" value="${backSectionForm.tradableEditTtableVoMap}"/>
<input id="id" name="id" type="hidden" value="${recommendInfoForm.returnId}"/>
<input type="hidden" id="editTableVString" name="editTableVString" value="${recommendInfoForm.editTableVString}"/>
   <div class="shenqing2">
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
	     <tr>
            <td width="61">债权编号：</td>
            <td width="150px" style="color:#2E74D1;"><input type="text" name="loanId" id="loanId" class="ggkj_inputtext_80" value="${backSectionForm.loanId}" /></td>
            <td width="85">债务人：</td>
            <td width="150px" style="color:#2E74D1;"><input type="text" name="loanCustomerName" id="loanCustomerName" class="ggkj_inputtext_80" value="${backSectionForm.loanCustomerName}" />
            </td>
			<td width="85">收益率：</td>
             <td width="250px" style="color:#2E74D1;">
             	<input type="text" id="irrMin" name="irrMin" value="${backSectionForm.irrMin}"  class="ggkj_inputtext_80" />-
             	<input type="text" id="irrMax" name="irrMax" value="${backSectionForm.irrMax}"  class="ggkj_inputtext_80"/>
           </td>
			<td width="85">借款产品：</td>
            <td width="150px" style="color:#2E74D1;">
             <select id="product"  name="product">
             <option value="" selected="true">请选择</option>
             <option value="1001" <c:if test="${backSectionForm.product=='1001'}">selected="true" </c:if>>随房贷</option>
	         <option value="1002" <c:if test="${backSectionForm.product=='1002'}">selected="true" </c:if>>随意贷</option>
	         <option value="1003" <c:if test="${backSectionForm.product=='1003'}">selected="true" </c:if>>随薪贷</option>
	         <option value="1004" <c:if test="${backSectionForm.product=='1004'}">selected="true" </c:if>>随车贷</option>
	         <option value="1005" <c:if test="${backSectionForm.product=='1005'}">selected="true" </c:if>>其他</option>   	
				</select>
            </td>
			 <td width="93"><input name="Input" type="submit" class="chaxun_btn" value="&nbsp;&nbsp;查询"/>
             </td>
          </tr>
      </table>
    </div>
	<p style="padding-left:16px;">
		<input type="button" onclick="recommendSubmit();" disabled="true" id="recommendButton" name="recommendButton" value="撮合" style="border:1px solid #3b97e4; background:#fff; display:block; width:80px; height:20px; line-height:20px; background:#c6defa; color:#000; float:left; margin-top:10px;"/>
	</p>
    <div style="clear:both;"></div>
	<div class="yiban">
	<table style="width:100%;" class="ggkj_table"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <th scope="col">序号<input type="checkbox" id = "num" name="num" onclick="EditTableAllChecked('tradbleCheckbox','num');"/></th>
    <th scope="col">债权编号</th>
    <th scope="col">债权人</th>
    <th scope="col">债务本金</th>
    <th scope="col">可推荐比例</th>
    <th scope="col">可推荐金额</th>
    <th scope="col">还款期数</th>
    <th scope="col">剩余期数</th>
    <th scope="col">irr</th>
	<th scope="col">还款日</th>
    <th scope="col">借款产品</th>
	<th scope="col">推荐金额</th>
    <th scope="col">推荐比例</th>
  </tr>
	<c:forEach items="${page.result}" var="item" varStatus="stauts">
		<c:choose>
  			<c:when test="${stauts.count%2==0}">
  				<tr class="ggkj_tdColr">
  			</c:when>
  			<c:otherwise>
  				<tr >
  			</c:otherwise>
		</c:choose>
				<td>${stauts.count+(page.pageNo-1)*10}
					<div style="display:none;"  id="divNo${item.ftTSellOrderDtl.id}">${stauts.count+(page.pageNo-1)*10}</div>
					<input type="checkbox" id="tradbleCheckbox" name="tradbleCheckbox" value="${item.ftTSellOrderDtl.id}" onclick="EditTableCheckBox('tradbleCheckbox','num');" <c:if test="${tradableMap[item.ftTSellOrderDtl.id].recommendScal!=null}">checked=true </c:if>/>
				</td>
			    <td height="34">
			    	${item.loanId}
			    	<input id="id${item.ftTSellOrderDtl.id}" name="id${item.ftTSellOrderDtl.id}" value="${item.loanInfo.loanID}" type="hidden"/>
			    	<input id="pv${item.ftTSellOrderDtl.id}" name="pv${item.ftTSellOrderDtl.id}" value="${item.pv}" type="hidden"/>
			    	<input id="version${item.ftTSellOrderDtl.id}" name="pv${item.ftTSellOrderDtl.id}" value="${item.verson}" type="hidden"/>
			    	<input id="calRecAmt${item.ftTSellOrderDtl.id}" name="calRecAmt${item.ftTSellOrderDtl.id}" value="${item.calRecAmt}" type="hidden"/>
			    	<input id="sellorderdtlId${item.ftTSellOrderDtl.id}" name="pv${item.ftTSellOrderDtl.id}" value="${item.ftTSellOrderDtl.id}" type="hidden"/>
			    </td>
			    <td>${item.loanInfo.customer.name}<div style="display:none;"  id="divcustomer${item.ftTSellOrderDtl.id}">${item.loanInfo.customer.name}</div></td>
			    <td>${item.loanInfo.loanAMt}<div style="display:none;"  id="divloanAMt${item.ftTSellOrderDtl.id}">${item.loanInfo.loanAMt}</div></td>
			    <td><fmt:formatNumber value="${item.tradableScale}" pattern="#0.####"/><div style="display:none;"  id="divtradableScale${item.ftTSellOrderDtl.id}">${item.tradableScale}</div></td>
				<td><fmt:formatNumber value="${item.calRecAmt}" pattern="##,###.##"/><div style="display:none;"  id="divcalRecAmt${item.ftTSellOrderDtl.id}">${item.calRecAmt}</div></td>
				<td>${item.loanInfo.totalNum}<div style="display:none;"  id="divtotalNum${item.ftTSellOrderDtl.id}">${item.loanInfo.totalNum}</div></td>
				<td>${item.loanInfo.totalNum - item.loanInfo.currNum}<div style="display:none;"  id="divdiffNum${item.ftTSellOrderDtl.id}">${item.loanInfo.totalNum - item.loanInfo.currNum}</div></td>
			    <td>${item.irr}<div style="display:none;"  id="divirr${item.ftTSellOrderDtl.id}">${item.irr}</div></td>
				<td><dictionary:display code="${item.investInfo.loanReturnDate}"/><div style="display:none;"  id="divloanReturnDate${item.ftTSellOrderDtl.id}">${item.investInfo.loanReturnDate}</div></td>
				<td>
					<c:if test="${item.loanCode=='1001'}">
						    	随房贷
					</c:if>
					<c:if test="${item.loanCode=='1002'}">
						    	随意贷
					</c:if>
					<c:if test="${item.loanCode=='1003'}">
						    	随薪贷
					</c:if>
					<c:if test="${item.loanCode=='1004'}">
						    	随车贷
					</c:if>
					<c:if test="${item.loanCode=='1005'}">
						    	其他
					</c:if><div style="display:none;"  id="divloanCode${item.ftTSellOrderDtl.id}">${item.loanCode}</div>
				</td>
				<td><input type="text" style="width:80px;" id="recommendAmt${item.ftTSellOrderDtl.id}" onblur="CheckAmt(this.value);" onkeyup="CalculateLoan(${item.ftTSellOrderDtl.id})" value="${tradableMap[item.ftTSellOrderDtl.id].recommendAmt}"/></td>
				<td>
					<div  id="recommendScal${item.ftTSellOrderDtl.id}"><fmt:formatNumber value="${tradableMap[item.ftTSellOrderDtl.id].recommendScal==null?0:tradableMap[item.ftTSellOrderDtl.id].recommendScal}" pattern="#0.####" /></div>
					<div style="display:none;" id="divrecommendScal${item.ftTSellOrderDtl.id}"><font color='red'>推荐金额不能大于可推荐金额!</font></div>
				</td>
			  </tr>
  </c:forEach>
</table>
	
</div>
<div class="ggkj_pagecontainer">
  <div class="ggkj_pageBGleft">
    	<span class="ggkj_pageBGright"></span>
    	<a href="javascript:jumpPage(1)" class="ggkj_pageUpa">
        
        </a>
        <a href="javascript:jumpPage(${page.prePage})" class="ggkj_pageUp">
        
        </a>
        <span class="ggkj_insertline"></span>
        <span class="ggkj_pageSpan">
        	页<input type="text" id="pageNo" name="pageNo" class="ggkj_inputtext_30" value="${page.pageNo}">OF ${page.totalPages}
        </span>
        <span class="ggkj_insertline"></span>
        <a href="javascript:jumpPage(${page.nextPage})" class="ggkj_pageDown">
        
        </a>
        <a href="javascript:jumpPage(${page.totalPages})" class="ggkj_pageDowna">
        
        </a>
        <span class="ggkj_insertline"></span>
        
    </div>
</div>

<script>
function jumpPage(pageNo) {
	var VString = AssemblyStringFromMapV(parent.RecommValueMap);
	//GetDOMById('editTableVString').value = VString;
	//parent.document.getElementById('pagetempdata').value = VString;
	
	//var pagetempdata = parent.document.getElementById('pagetempdata').value;
	//GetDOMById('editTableVString').value = pagetempdata;
	
	GetDOMById('editTableVString').value = VString;
	
	document.getElementById('pageNo').value=pageNo;
	document.getElementById('mainForm').submit();
}

//全选Checkbox
function EditTableAllChecked(checkboxname,numCheckboxId) {
    var ids = document.getElementsByName(checkboxname);
    var nameArray = getRecommendNameArray();
    
    if (GetDOMById(numCheckboxId).checked == true) {
        for (var i = 0; i < ids.length; i++) {
            ids[i].checked = true;
        }
        parent.RecommValueMap = EditTableOnclickPutValue(checkboxname,parent.RecommValueMap,nameArray);
    } else {
        for (var i = 0; i < ids.length; i++) {
            ids[i].checked = false;
            var keyValue = ids[i].value.toString();
            parent.RecommValueMap.remove(keyValue);
        }
    }
}

function getRecommendNameArray(){
	var nameArray = new Array();
	nameArray[0]='recommendAmt:input';
	nameArray[1]= 'sellorderdtlId:input';
	nameArray[2]='divNo:div';
	nameArray[3]='version:input';		
	nameArray[4]='pv:input';
	nameArray[5]='id:input';
	nameArray[6]='calRecAmt:input';
	nameArray[7]='divcustomer:div';
	nameArray[8]='divloanAMt:div';
	nameArray[9]='divtradableScale:div';
	nameArray[10]='divcalRecAmt:div';
	nameArray[11]='divtotalNum:div';
	nameArray[12]='divdiffNum:div';
	nameArray[13]='divirr:div';
	nameArray[14]='divloanReturnDate:div';
	nameArray[15]='divloanCode:div';
	nameArray[16]='recommendScal:div';
	return nameArray;
}

/**
 *  勾选数据(单选CheckBox)
 */
function EditTableCheckBox(checkboxname,numCheckboxId){
	var ids = document.getElementsByName(checkboxname);
	var nameArray = getRecommendNameArray();
	for (var i = 0; i < ids.length; i++) {
	   var keyValue = ids[i].value.toString();
       if(ids[i].checked ==true){
       		parent.RecommValueMap = EditTableOnclickPutValue(checkboxname,parent.RecommValueMap,nameArray);
       }else{
       		parent.RecommValueMap.remove(keyValue);
       }
    }
    //判断是否全选中
    var NumIsChecked = true;
    for(var i = 0; i < ids.length; i++){
    	if(ids[i].checked ==false){
    		NumIsChecked = false;
    		break;
    	}
    }
    if(NumIsChecked==true){
    	GetDOMById(numCheckboxId).checked=true;
    }else{
        GetDOMById(numCheckboxId).checked=false;
    }
    
    if(parent.RecommendErrorData.arr.length>0){
		SettingButtonStyle('recommendButton',true);
	}else{
		SettingButtonStyle('recommendButton',false);
	}
}

 
 
var isOutSide = false;
function CalculateLoan(sellorderdtlId){

	var inputAmt =GetInputValue('recommendAmt'+sellorderdtlId);//输入金额
	var pv = GetInputValue('pv'+sellorderdtlId);//PV值
	var calRecAmt =GetInputValue('calRecAmt'+sellorderdtlId);//推荐金额
	
	
	var scal = FormatNumber(inputAmt/pv,4);
	var diffAmt = inputAmt - calRecAmt ;
	SetDivValue('recommendScal'+sellorderdtlId,scal);
	
	var checkboxname = 'tradbleCheckbox';
	var nameArray = getRecommendNameArray();
	var key=sellorderdtlId.toString() ;
	parent.RecommValueMap = EditTablePutValue(parent.RecommValueMap,nameArray,key);
	
	if(diffAmt>0){
		GetDOMById('divrecommendScal'+sellorderdtlId).style.display = '';
		parent.RecommendErrorData = EditTablePutValue(parent.RecommendErrorData,nameArray,key);
	}else{
		parent.RecommendErrorData.remove(key);
		GetDOMById('divrecommendScal'+sellorderdtlId).style.display = 'none';
	}
	
	if(parent.RecommendErrorData.arr.length>0){
		SettingButtonStyle('recommendButton',true);
	}else{
		SettingButtonStyle('recommendButton',false);
	}
} 

function checkrecommendData(){
	var totalAmt = 0;
	var amt = 0;
	var investAmt = parent.document.getElementById('recommentAmt').value;
	amt  = investAmt;
	
	var checkboxname = 'tradbleCheckbox';
	var nameArray = getRecommendNameArray();
	var key ;
	
	for(var m=0;m<parent.RecommValueMap.arr.length;m++){
		key = parent.RecommValueMap.arr[m].key;
		totalAmt = totalAmt + Number(parent.RecommValueMap.arr[m].value[0]);
		if(parent.RecommValueMap.arr[m].value[0]==0){
			alert('撮合债权中含有零值的投资!');
			parent.RecommendErrorData = EditTablePutValue(parent.RecommendErrorData,nameArray,key);
			return false;
		}else{
			parent.RecommendErrorData.remove(key);
		}
	}
	
	if(Number(amt)!=totalAmt.toFixed(2) && parent.RecommValueMap.arr.length>0){
		alert("总推荐金额不等于投资金额 (投资金额为: "+amt+",总推荐金额为:"+totalAmt.toFixed(2)+",差值:"+FormatNumber((Number(amt) - totalAmt.toFixed(2)),2)+")");
		return false;
	}
}

function recommendSubmit(){
	//var VString = AssemblyStringFromMapV(parent.RecommValueMap);
	//SetValue('editTableVString',VString);
	//document.getElementById('mainForm').submit();
	
	var isTrue = checkrecommendData();
	if(isTrue==false){
		return;
	}
		
	parent.mTab_iframe('${ctx}/backsection/backsection_hasrecommend','1');
}
function doSubmit(){
	var patrn=/[0-9]+[.[0-9]+]?/;
	var irrMin=document.getElementById("irrMin");
	var irrMax=document.getElementById("irrMax");
	if(irrMin.value!=""){
		if (!patrn.test(irrMin.value)) {
			alert("收益率必须是正小数！");
			return;
		}
	} 
	if(irrMax.value!=""){
		if (!patrn.test(irrMax.value)) {
			alert("收益率必须是正小数！");
			return;
		}
	}
	$("#pageNo").val(1);
	document.getElementById('mainForm').submit(); 
}
$(document).ready(function(){
	var VString = AssemblyStringFromMapV(parent.RecommValueMap);
	SetValue('id',parent.document.getElementById('returnId').value);
	SetValue('editTableVString',VString);
	
	
	
	var tag = '${backSectionForm.tag}';
	if(tag=='action'){
		var isTrue = checkrecommendData();
		if(isTrue==false){
			return;
		}
		document.getElementById('mainForm').submit();
	}
});

</script>
</form:form>
 </body>
 </html> 